/**
 * TabVD is a Virtual Desktop Client/Server designed specifically for connecting
 * from Tablet like devices.
 *
 * Copyright (C) 2011 Subhabrata Ghosh
 *
 * This program is free software: you can redistribute it and/or modify it under
 * the terms of the GNU General Public License as published by the Free Software
 * Foundation, either version 3 of the License, or (at your option) any later
 * version.
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
 * details.
 *
 * You should have received a copy of the GNU General Public License along with
 * this program. If not, see <http://www.gnu.org/licenses/>.
 *
 * FileLogger.h
 *
 *  Created on: May 27, 2011
 *      Author: subhagho
 */

#ifndef FILELOGGER_H_
#define FILELOGGER_H_

#include <iostream>
#include <fstream>

#include "AbstractLogger.h"
#include "AbstractConfiguration.h"
#include "FileUtils.H"

namespace tabvd {

    class FileLogger: public tabvd::AbstractLogger {
        public:
            FileLogger();
            virtual ~FileLogger();

            /**
             * \brief Initialize the file logger.
             *
             * @param config - Configuration handle containing the file logger configurations.
             */
            virtual void init(AbstractConfiguration* config);

            /**
             * \brief - Print to the log file.
             *
             * Semantics of the format is similar to (f)printf()
             *
             * @param format - Print format definition.
             */
            virtual void print(const char* format, ...);

            virtual void log(ELevel level, const char* format, ...);

            /**
             * \brief Clear the contents of the log file.
             */
            virtual void clear();

            /**
             * \brief Backup the file.
             *
             * Backs up the file to the logging directory, suffixed with the date.
             */
            virtual void backup();


            virtual bool createDefault(AbstractConfiguration* config);

            virtual bool update(AbstractConfiguration* config);

            virtual std::string getfilename();
        protected:
            const AbstractConfiguration* config;
            std::string* filename;
            std::string* directory;
            std::ofstream* stream;
            
            int maxsize;
            bool dobackup;

            static const std::string _CONFIG_KEY_DIRECTORY_;
            static const std::string _CONFIG_KEY_FILENAME_;
            static const std::string _CONFIG_KEY_LEVEL_;
            static const std::string _CONFIG_KEY_MAXSIZE_;
            static const std::string _CONFIG_KEY_BACKUP_;

            /* --------------------------------------------------------------------------*/
            /**
            * @Synopsis  Check the size of the log file and recycle/backup if required.
            *
            * @Returns   
            */
            /* ----------------------------------------------------------------------------*/
            virtual bool checksize(bool);

            /* --------------------------------------------------------------------------*/
            /**
            * @Synopsis  Open/ReOpen the file output stream.
            *
            * @Returns   
            */
            /* ----------------------------------------------------------------------------*/
            virtual bool openstream();

            /* --------------------------------------------------------------------------*/
            /**
            * @Synopsis  Close the current output stream.
            *
            * @Returns   
            */
            /* ----------------------------------------------------------------------------*/
            virtual bool closestream();
    };

    const std::string FileLogger::_CONFIG_KEY_DIRECTORY_ = "FileLogger.Config.Directory";
    const std::string FileLogger::_CONFIG_KEY_FILENAME_ = "FileLogger.Config.Filename";
    const std::string FileLogger::_CONFIG_KEY_LEVEL_ = "FileLogger.Config.LogLevel";
    const std::string FileLogger::_CONFIG_KEY_MAXSIZE_ = "FileLogger.Config.MaxSize";
    const std::string FileLogger::_CONFIG_KEY_BACKUP_ = "FileLogger.Config.Backup";

}

#endif /* FILELOGGER_H_ */
